package de.ds.codematurity.internal;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;

/**
 * Immutable (not 100%) implementation of a code maturity color. A
 * CodeMaturityColor always has a SWT Color and keyword to which it fits. There
 * is also a flag which says whether the case of the keyword is ignored
 * 
 * @author Daniel
 * 
 */
public class CodeMaturityColor {

	Color color;
	String keyword;
	boolean ignoreCase;

	/**
	 * Get the SWT Color
	 * 
	 * @return
	 */
	public Color getColor() {
		return color;
	}

	/**
	 * Check if the given word fits to the keyword. If ignorecase is true, the
	 * case is ignored.
	 * 
	 * @param keyword
	 * @return
	 */
	public boolean keywordFits(String keyword) {
		if (ignoreCase) {
			if (this.keyword.equalsIgnoreCase(keyword)) {
				return true;
			}
		} else {
			if (this.keyword.equals(keyword)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Create a new Code Maturity Color
	 * 
	 * @param color
	 * @param keyword
	 * @param ignoreCase
	 * @throws IllegalArgumentException
	 *             of one of the input objects is null
	 */
	public CodeMaturityColor(Color color, String keyword, boolean ignoreCase) {
		if (color == null || keyword == null || keyword.isEmpty()) {
			throw new IllegalArgumentException(
					"CodeMaturityColor Constructor was invoked with the arguments:Color="
							+ color + " keyword=" + keyword + "ignoreCase="
							+ ignoreCase);
		}
		this.color = color;
		this.keyword = new String(keyword);
		this.ignoreCase = ignoreCase;
	}

	/**
	 * Get the default color. Should be white.
	 * 
	 * @return
	 */
	public static CodeMaturityColor getDefault() {
		return new CodeMaturityColor(new Color(Display.getDefault(), 255, 255,
				255), "default", true);
	}
}
